package com.example.ui.activity

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.R
import com.example.adapter.RecycleViewAdapterAdapter
import com.example.entity.Planet
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import jp.wasabeef.recyclerview.animators.ScaleInAnimator
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator


class RecycleViewActivity : AppCompatActivity() {

    private lateinit var planetList: MutableList<Planet>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_recycle_view)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)

        // 1. 获取 Planet 数据列表（例如通过 Planet.getPlanets()）
        planetList = Planet.getPlanets().toMutableList()
        // 2. 初始化适配器，传入上下文和数据列表
        val adapter = RecycleViewAdapterAdapter(this, planetList)
        // 3. 设置 RecyclerView 的布局管理器和适配器
//        recyclerView.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
//        recyclerView.layoutManager = GridLayoutManager(this, 3)
        val staggeredLayoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        recyclerView.layoutManager = staggeredLayoutManager
        // 4. 设置 RecyclerView 的分隔线
        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                super.getItemOffsets(outRect, view, parent, state)
                outRect.bottom = 5
            }
        })
        // 5. 设置 RecyclerView 的动画效果
        // 示例：淡入动画 + 横向滑动
//        recyclerView.itemAnimator = SlideInLeftAnimator()
        recyclerView.itemAnimator = ScaleInAnimator()

        recyclerView.adapter = adapter

        val refreshLayout = findViewById<View>(R.id.refreshLayout) as RefreshLayout
        refreshLayout.setRefreshHeader(ClassicsHeader(this))
        refreshLayout.setRefreshFooter(ClassicsFooter(this))
        refreshLayout.setOnRefreshListener { refreshlayout ->
            // 额外添加一条新数据
            val newPlanet = Planet(R.drawable.qqq, "New Refresh Title", "New Refresh Content")
            planetList.add(0, newPlanet) // 头部添加
//            planetList.removeAt(0)
            adapter.notifyDataSetChanged()  // 不推荐，全部更新性能消耗太大
//            adapter.notifyItemRangeChanged(0, planetList.size)
//            adapter.notifyItemChanged(0)  // 这是更新的方法，添加和删除有他们对应的insert和remove方法
            adapter.notifyItemInserted(0)
//            adapter.notifyItemRangeInserted(0,1)
//            adapter.notifyItemRemoved(0)
//            adapter.notifyItemRangeRemoved(0,1)
            refreshlayout.finishRefresh(2000 /*,false*/) //传入false表示刷新失败
        }
        refreshLayout.setOnLoadMoreListener { refreshlayout ->
            // 额外添加一条新数据
            val newPlanet = Planet(R.drawable.qqq, "New Refresh Title", "New Refresh Content")
//            planetList.add( newPlanet) // 头部添加
            planetList.removeAt(planetList.size-1)
//            adapter.notifyDataSetChanged()  // 不推荐，全部更新性能消耗太大
//            adapter.notifyItemChanged(planetList.size)  // // 这是更新的方法，添加和删除有他们对应的insert和remove方法
//            adapter.notifyItemInserted(planetList.size)
//            adapter.notifyItemRangeInserted(planetList.size-1,1)
//            adapter.notifyItemRemoved(planetList.size)
            adapter.notifyItemRangeRemoved(planetList.size,1)
            refreshlayout.finishLoadMore(200 /*,false*/) //传入false表示加载失败
        }
    }
}